using System;
using System.Data;
using System.Reflection;
using System.Windows.Forms;
using ctsu.BaseForms;

namespace ctsu.Admin.BusLogic
{

	public class SearchUtils
	{
		public SearchUtils()
		{

		}

		public static void SetColumnMeta(DataTable dt, SearchResultInfo si)
		{
			dt.TableName = si.TableName;
			foreach (DataColumn dc in dt.Columns)
			{
				ReportColumn rc = si.SearchResultColsH[dc.ColumnName] as ReportColumn;
				dc.Caption = rc.DisplayName;
				dc.ExtendedProperties.Add("Width", rc.ColumnLength);
			}
		}

		public static FormSearchResultsBase GetSearchResultsDialog(SearchResultInfo si, DataTable dt)
		{
			//create an instance of the form
			Type formType = Type.GetType("ctsu.BaseForms." + si.SearchClassName.Trim() + ",ctsu.BaseForms");
			if (formType == null)
			{
				MessageBox.Show("This function is not defined yet");
				return null;
			}

			//invoke creation of form instance with a null constructor
			ConstructorInfo info = formType.GetConstructor(Type.EmptyTypes);
			BaseForms.FormSearchResultsBase fs = (FormSearchResultsBase)info.Invoke(null);

			//create an instance of the business logic
			Type[] argTypes = new Type[3];
			argTypes[0] = Type.GetType("ctsu.BaseForms.FormSearchResultsBase,ctsu.BaseForms");
			argTypes[1] = typeof(DataTable);
			argTypes[2] = typeof(DataGrid);

			object[] args = new object[3];
			args[0] = fs;
			args[1] = dt;
			args[2] = fs.dg;

			//invoke creation of bl
			Type busLog = Type.GetType("ctsu.Admin.BusLogic." + si.BLClassName);

			info = busLog.GetConstructor(argTypes);
			IBLSearchResultsBase iBL = (IBLSearchResultsBase)info.Invoke(args);

			//create an instance of the controller
			//pass form and bl to the constructor
			argTypes = new Type[1];
			argTypes[0] = Type.GetType("ctsu.BaseForms.IBLSearchResultsBase,ctsu.BaseForms");
			args = new object[1];
			args[0] = iBL;

			//invoke creation of controller
			Type contrlr = Type.GetType("ctsu.Admin.BusLogic." + si.SearchController);
			info = contrlr.GetConstructor(argTypes);
			ISearchResultsController isc = (ISearchResultsController)info.Invoke(args);
			fs.Controller = isc;
			iBL.Initialize();
			return fs;
		}

		public static FormSearchBase GetSearchDialog(SearchInfo si)
		{
			//create an instance of the form
			Type formType = Type.GetType("ctsu.Admin.Forms." + si.SearchClassName.Trim());
			if (formType == null)
			{
				MessageBox.Show("This function is not defined yet");
				return null;
			}

			//invoke creation of form instance with a null constructor
			ConstructorInfo info = formType.GetConstructor(Type.EmptyTypes);
			FormSearchBase fs = (FormSearchBase)info.Invoke(null);

			//set the title bar
			fs.Text = si.TableName + " Search";

			//get the search fields			
			foreach (ReportColumn rc in si.SearchFields)
				fs.SearchFields.Items.Add(rc);

			//design list view for result fields			
			ListView lvw = fs.SearchResults;
			lvw.Columns.Clear();

			if (lvw != null)
			{
				//				DataColumn dc;
				//				string fieldType;				
				foreach (ReportColumn rc in si.SearchResultCols)
				{
					ColumnHeader ch = new ColumnHeader();
					ch.Text = rc.DisplayName;

					//get width from schema
					//					dc = si.TableSchema.Columns[src.FieldName];
					//					fieldType = dc.DataType.ToString();

					//auto size column based on display text
					ch.Width = -2;
					lvw.Columns.Add(ch);
				}
			}

			SearchController sc = new SearchController(fs, si);
			//create an instance of the controller
			//pass form and bl to the constructor
			Type[] argTypes = new Type[2];
			argTypes[0] = Type.GetType("ctsu.Admin.Forms.FormSearch,Caap");
			argTypes[1] = Type.GetType("ctsu.BaseForms.SearchInfo,ctsu.BaseForms");

			//TODO - you don't need to pass the form... you can access it thru the iBL
			object[] args = new object[2];
			args[0] = fs;
			args[1] = si;

			//invoke creation of controller
			Type srchContrlrType = Type.GetType("ctsu.Admin.BusLogic." + si.SearchController);
			info = srchContrlrType.GetConstructor(argTypes);
			ISearchController isc = (ISearchController)info.Invoke(args);

			fs.Controller = isc;
			return fs;
		}
	}
}
